package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.StrategyConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/*
 *@Description:扣费策略
 *@author:刘哥
 *@Date:86180 2022/10/19 17:46
 */
@Service("fee")
@Slf4j
public class FeeServiceImpl implements FilterChainService {
    @Autowired
    private CacheFeign cacheFeign;
//    给mq推送的工具类
    @Autowired
    private PushMsgUtil pushMsgUtil;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-扣费策略] 开始执行==========");
//        获取用户当前用户余额
        Map<String,String> clientMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_CUSTOMER_FEE + standardSubmit.getClientID());
        String feeCount = clientMap.get("fee");
        Long count = Long.parseLong(feeCount);
//        获取当前短信的费用
        Long fee = standardSubmit.getFee();
//        判断余额是否足够付费,不够不让发送短信
        if(count < fee){
            log.info("[策略模块-扣费策略] 执行失败,账户余额不足请及时充值==========");
//            将费用不足的信息发送到日志队列,供搜索模块使用
            pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_FEE);
//             将费用不足的信息发送到report推送队列,供接口模式推送给用户项目使用
            pushMsgUtil.pushReport(standardSubmit,StrategyConstants.STRATEGY_ERROR_FEE);
            return false;
        }else{
//        扣费
            cacheFeign.hincr(CacheConstants.CACHE_PREFIX_CLIENT+standardSubmit.getClientID(),"fee",-fee);
        }


        log.info("[策略模块-扣费策略] 执行完毕==========");
        return true;
    }
}
